package com.suneee.store.dao.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.base.util.ContextHelper;
import com.suneee.context.page.Page;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.store.bean.ViewStockRecordDTO;
import com.suneee.store.dao.ViewStockRecordDao;
import com.suneee.store.entity.ViewStockRecord;
import com.suneee.store.resources.StaticParam;

@Repository("viewStockRecordDao")
public class ViewStockRecordDaoImpl extends GenericDao<ViewStockRecord, String>implements ViewStockRecordDao {

	@Override
	public Page<Map<String,Object>> viewStockRecordPage(ViewStockRecordDTO dto) {
		// TODO Auto-generated method stub
		WhereClauseBuilder sbf = this.wrapWhereSQL(dto);
		DecimalFormat decimalFormat=new DecimalFormat("##0.00");
		Page<Map<String, Object>> list   =this.queryForList(sbf.getSql(), dto, sbf.getArgs());
		 if(list != null&&list.getItems().size()>0){
			 for (int i = 0; i < list.getItems().size(); i++) {
				 if(list.getItems().get(i).get("marketPrice")!=null)list.getItems().get(i).put("marketPrice", decimalFormat.format(Float.parseFloat(list.getItems().get(i).get("marketPrice").toString())));
				 if(list.getItems().get(i).get("price")!=null)list.getItems().get(i).put("price", decimalFormat.format(Float.parseFloat(list.getItems().get(i).get("price").toString())));
				 if(list.getItems().get(i).get("advicePrice")!=null)list.getItems().get(i).put("advicePrice", decimalFormat.format(Float.parseFloat(list.getItems().get(i).get("advicePrice").toString())));
			 }
		 }
		//return this.queryForList(sbf.getSql(), dto, sbf.getArgs());
		return list;
	}

	private WhereClauseBuilder wrapWhereSQL(ViewStockRecordDTO dto){
		/*Integer adminId = ContextHelper.getCurrentLoginInfo().getAdminId();
		List<Map<String,Object>> shopList = this.queryForList("select shop_id from t_shop_admin where admin_id = ?", adminId);
		List<Object> list = null;
		if(null != shopList && shopList.size() > 0)
		{
			list = new ArrayList<Object>();
			for(Map<String,Object> map :shopList)
			{
				list.add(map.get("shop_id"));
			}	
		}
		if(null == shopList || shopList.size() == 0)
			{
				list = new ArrayList<Object>();
				list.add(" ");
			}
		*/
		String sql = "SELECT "
				+ "re.record_id AS recordId,"
				+ "re.operate_store_id AS operateStoreId,"
				+ "re.relevant_store_id AS relevantStoreId,"
				+ "re.record_type AS recordType,"
				+ "re.create_time AS createTime,"
				+ "re.remark,"
				+ "re.stock_type AS stockType,"
				+ "re.channel_code AS channelCode,"
				+ "re.operate_channel_code AS operateChannelCode,"
				+ "re.relevant_channel_code AS relevantChannelCode,"
				+ "cha.`name` AS channelName,"
				+ "sp.`name` AS operateStoreName,"
				+ "spp.`name` AS relevantStoreName,"
				+ "de.spu_id AS spuId,"
				+ "de.sku_id AS skuId,"
				+ "de.price,"
				+ "de.price AS advicePrice,"
				+ "de.incoming AS incoming,"
				+ "spu.title AS title,"
				+ "spu.market_price AS marketPrice,"
				+ "spu.from_type AS fromType,"
				+ "sku.price AS price,"
				+ "sku.purchase_price AS purchasePrice,"
				+ "sku.spec_one_value AS specOneValue,"
				+ "sku.spec_two_value AS specTwoValue,"
				+ "sku.spec_three_value AS specThreeValue,"
				+ "sku.spec_four_value AS specFourValue,"
				+ "adm.account AS account,"
				+ "cat.`name` AS category"
				+ " FROM"
				+ " t_stock_record re"
				+ " LEFT JOIN t_shop sp  ON sp.shop_id = re.operate_store_id "
				+ " LEFT JOIN t_shop spp ON spp.shop_id = re.relevant_store_id"
				+ " LEFT JOIN t_stock_detail de on de.record_id = re.record_id"
				+ " LEFT JOIN t_spu spu on spu.spu_id = de.spu_id"
				+ " LEFT JOIN t_sku sku on sku.sku_id = de.sku_id"
				+ " LEFT JOIN t_category cat on cat.category_code = spu.category_id"
				+ " LEFT JOIN t_administrator adm on adm.admin_id =  re.create_user_id "
				+ " LEFT JOIN t_channel cha on cha.channel_code = re.channel_code"
				+ " WHERE  re.record_id = de.record_id "
				+ " AND  re.operate_store_id = sp.shop_id "
				+ " AND  re.relevant_store_id = spp.shop_id"
				+ " AND de.spu_id = spu.spu_id" + " AND de.sku_id = sku.sku_id"
				+ " AND spu.category_id = cat.category_code"
				+ " AND re.create_user_id = adm.admin_id "
				+ " AND re.channel_code = cha.channel_code ";
		WhereClauseBuilder sbf =  new WhereClauseBuilder(sql);
		sbf.equals("re.record_id",dto.getRecordId());
		sbf.equals("re.operate_store_id",dto.getOperateStoreId());
		sbf.equals("re.record_type",dto.getRecordType());
		sbf.equals("re.stock_type",dto.getStockType());
		sbf.equals("de.spu_id",dto.getSpuId());
		sbf.equals("de.sku_id",dto.getSkuId());
		sbf.contains("spu.title",dto.getTitle());
		sbf.equals("spu.category_id",dto.getCategoryCode());
		sbf.equals("re.enterprise_code", dto.getEnterpriseCode());
		sbf.contains("adm.account", dto.getAccount());
		sbf.between("re.create_time", dto.getCreateTimeStart(), dto.getCreateTimeEnd());
//		sbf.noEquals("incoming", dto.getIncoming());
		sbf.equals("re.relevant_store_id", dto.getRelevantStoreId());
		//sbf.in("relevant_store_id", list);
		sbf.equals("spu.from_type",dto.getFromType());
		
		if("总仓入库".equals(dto.getRecordType())){
			sbf.equals("re.relevant_channel_code",dto.getRelevantChannelCode());
		}
		if("总仓出库".equals(dto.getRecordType())){
			sbf.equals("re.operate_channel_code",dto.getOperateChannelCode());
		}
		
		Object[] obj = null;
		sbf.append(" ORDER BY re.create_time DESC ", obj);
		return sbf;
	}

	@Override
	public ViewStockRecord getRecordByRecordIdAndSkuId(String recordId, Integer skuId) {
		// TODO Auto-generated method stub
		//String sql = " select * from v_stock_record where 1=1 and record_id = ? and sku_id = ?";
		WhereClauseBuilder build = new WhereClauseBuilder();
		build.equals("record_id", recordId);
		build.equals("sku_id", skuId);
//		Object[] args = new Object[2];
//		args[0] = recordId;
//		args[1] = skuId;
		//this.queryForList(build.getSql(),build.getArgs());
		
		
		List<ViewStockRecord> list = this.selectWhere(build.getSql(),build.getArgs());;
		if(list == null || list.isEmpty())
			return null;
		return list.get(0);
	}

	@Override
	public List<String> getRecordIdList(ViewStockRecordDTO dto) {
		// TODO Auto-generated method stub
		String sql = " select distinct record_id from v_stock_record where record_type = ? "
				+ "and operate_store_id = ? and enterprise_code = ? "
				+ "and (status = '" + StaticParam.SYNC_STATUS_WAITTING + "' "
				+ "or status = '" + StaticParam.SYNC_STATUS_FAIL + "') ";
		Object[] args = new Object[3];
		args[0] = dto.getRecordType();
		args[1] = dto.getOperateStoreId();
		args[2] = dto.getEnterpriseCode();
		List<Map<String, Object>> result = this.queryForList(sql, args);
		if(result == null || result.isEmpty())
			return null;
		List<String> strList = new ArrayList<>();
		for(Map<String, Object> map : result){
			strList.add((String)map.get("record_id"));
		}
		return strList;
	}

	@Override
	public Page<Map<String, Object>> getRecordIdPage(ViewStockRecordDTO dto) {
		Integer adminId = ContextHelper.getCurrentLoginInfo().getAdminId();
		String sql = " select distinct record_id from v_stock_record where record_type = ? "
				+ "and operate_store_id = ? and enterprise_code = ? and stock_type = ? "
				+"and relevant_store_id IN (SELECT shop_id FROM t_shop_admin WHERE admin_id = ?) "
				+ "and (status = '" + StaticParam.SYNC_STATUS_WAITTING + "' "
				+ "or status = '" + StaticParam.SYNC_STATUS_FAIL + "') ORDER BY create_time DESC ";
		Object[] args = new Object[]{
			dto.getRecordType(),
			dto.getOperateStoreId(),
			dto.getEnterpriseCode(),
			dto.getStockType(),
			adminId
		};
		return this.queryForList(sql, dto, args);
	}
	
}
